﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using DevExpress.XtraEditors;
using WarehouseManagement.BusinessLogic.BC;
using System.Linq;
using DevExpress.XtraSplashScreen;

namespace WarehouseManagement.Views.Export
{
    public partial class ViewRevenue : DevExpress.XtraEditors.XtraForm
    {
        private ImportBC _importBC;
        private ExportBC _exportBC;
        private RevenueBC _revenueBC;

        public ViewRevenue()
        {
            InitializeComponent();
            _importBC = new ImportBC();
            _exportBC = new ExportBC();
            _revenueBC = new RevenueBC();
        }

        private void radioGroup1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (radGrdViewAs.SelectedIndex == 0)
            {
                dateRange1.ViewStyle = Shared.DateRange.DateViewStyle.DayView;
            }
            else if (radGrdViewAs.SelectedIndex == 1)
            {
                dateRange1.ViewStyle = Shared.DateRange.DateViewStyle.MonthView;
            }
            else
            {
                dateRange1.ViewStyle = Shared.DateRange.DateViewStyle.YearView;
            }
        }

        private void ViewRevenue_Load(object sender, EventArgs e)
        {
            radGrdViewAs.SelectedIndex = 0;

        }

        private void gridControl1_Click(object sender, EventArgs e)
        {

        }

        private void btnView_Click(object sender, EventArgs e)
        {
            SplashScreenManager.ShowForm(typeof(WaitForm1));
            RenderChart();
            SplashScreenManager.CloseForm();
        }

        private void RenderChart()
        {
            chartControl1.Series.Clear();
            var ds = CreateDataSourceDaily().ToList();
            RenderLine("lnImport", "Nhập", 0, ds);
            RenderLine("lnExport", "Xuất", 1, ds, MarkerKind.Circle, DashStyle.Solid);
            RenderLine("lnProfit", "Lãi", 2, ds, MarkerKind.Square, DashStyle.DashDotDot);
        }

        private void RenderLine(string lineName,
            string legendText,
            int type,
            IEnumerable<ChartDataItem> dataSource,
            MarkerKind markerKind = MarkerKind.Triangle,
            DashStyle lineStyle = DashStyle.Dash
            )
        {
            var importSeries = new Series(lineName, ViewType.Line);

            importSeries.LegendText = legendText;
            importSeries.ValueScaleType = ScaleType.Numerical;

            importSeries.CrosshairLabelPattern = "{V:n}";  //<<
            importSeries.ShowInLegend = true;


            importSeries.DataSource = dataSource;
            importSeries.ArgumentDataMember = "ArgumentPoint";

            if (radGrdViewAs.SelectedIndex == 0)
            {
                importSeries.ArgumentScaleType = ScaleType.DateTime;
            }
            else if (radGrdViewAs.SelectedIndex == 1)
            {
                importSeries.ArgumentScaleType = ScaleType.Qualitative;
            }
            else
            {
                importSeries.ArgumentScaleType = ScaleType.Qualitative;
            }

            if (type == 0)
            {

                importSeries.ValueDataMembers.AddRange("ImportValuePoint");
            }
            else if (type == 1)
            {

                importSeries.ValueDataMembers.AddRange("ExportValuePoint");
            }
            else
            {
                importSeries.ValueDataMembers.AddRange("ProfitValuePoint");
            }
            // Access the view-type-specific options of the series.
            ((LineSeriesView)importSeries.View).LineMarkerOptions.Kind = markerKind;
            ((LineSeriesView)importSeries.View).LineStyle.DashStyle = lineStyle;
            ((LineSeriesView)importSeries.View).LineStyle.Thickness = 3;


            chartControl1.Series.Add(importSeries);
        }

        private IEnumerable<ChartDataItem> CreateDataSourceDaily()
        {


            if (radGrdViewAs.SelectedIndex == 0)
            {
                for (DateTime t = dateRange1.FromDate; t <= dateRange1.ToDate; t = t.AddDays(1))
                {
                    decimal ip, ep, pr;
                    ip = _importBC.GetTotalImportValueInDay(t.Date);
                    _exportBC.GetTotalExportValueInDay(t.Date, out ep, out pr);
                    yield return new ChartDataItem()
                    {
                        ArgumentPoint = t,
                        ImportValuePoint = ip,
                        ExportValuePoint = ep,
                        ProfitValuePoint = pr
                    };
                }
            }
            else if (radGrdViewAs.SelectedIndex == 1)
            {

                for (DateTime t = new DateTime(dateRange1.FromDate.Year, dateRange1.FromDate.Month, 1);
                    t <= new DateTime(dateRange1.ToDate.Year, dateRange1.ToDate.Month, 1);
                    t = t.AddMonths(1))
                {
                    decimal ip, ep, pr;
                    _revenueBC.GetMonthlyImportExportValue(t.Month, t.Year, out ip, out ep, out pr);
                    yield return new ChartDataItem()
                    {
                        ArgumentPoint = new DateTime(t.Year, t.Month, DateTime.DaysInMonth(t.Year, t.Month)),
                        ImportValuePoint = ip,
                        ProfitValuePoint = pr,
                        ExportValuePoint = ep
                    };
                }
            }
            else
            {
                for (DateTime t = new DateTime(dateRange1.FromDate.Year, 12,
                                    DateTime.DaysInMonth(dateRange1.FromDate.Year, dateRange1.FromDate.Month));
                                  t <= new DateTime(dateRange1.ToDate.Year, 12,
                                      DateTime.DaysInMonth(dateRange1.ToDate.Year, dateRange1.ToDate.Month));
                                  t = t.AddYears(1))
                {
                    decimal ip, ep, pr;
                    _revenueBC.GetYearlyImportExportValue(t.Year, out ip, out ep, out pr);
                    yield return new ChartDataItem()
                    {
                        ArgumentPoint = t,
                        ImportValuePoint = ip,
                        ProfitValuePoint = pr,
                        ExportValuePoint = ep
                    };
                }
            }
        }


        class ChartDataItem
        {
            public DateTime ArgumentPoint { get; set; }
            public decimal ImportValuePoint { get; set; }
            public decimal ExportValuePoint { get; set; }
            public decimal ProfitValuePoint { get; set; }
        }

        private void chartControl1_CustomDrawSeriesPoint(object sender, CustomDrawSeriesPointEventArgs e)
        {
            // Get the value of your point (Age in your case)
            var pointValue = e.SeriesPoint.DateTimeArgument;

            //// You can get the argument text using e.SeriesPoint.Argument
            //// Set the label text of your point
            //e.LabelText = "value is " + pointValue;
            //e.LegendText = "le " + pointValue;


        }

        private void chartControl1_CustomizeXAxisLabels(object sender, CustomizeXAxisLabelsEventArgs e)
        {
            //e.Axis.DateTimeOptions.Format = DateTimeFormat.Custom;
            //e.Axis.DateTimeOptions.FormatString = "dd/MM/yyyy";
        }

        private void chartControl1_CustomDrawAxisLabel(object sender, CustomDrawAxisLabelEventArgs e)
        {

            var name = e.Item.Axis.Name;
            if (name == "Primary AxisX")
            {
                if (radGrdViewAs.SelectedIndex == 0)
                    e.Item.Text = string.Format("{0:dd/MM/yyyy}", e.Item.AxisValue);
                else if (radGrdViewAs.SelectedIndex == 1)
                    e.Item.Text = string.Format("{0:MM/yyyy}", Convert.ToDateTime(e.Item.AxisValue));
                else
                    e.Item.Text = string.Format("{0:yyyy}", Convert.ToDateTime(e.Item.AxisValue));
            }
            else
            {
                e.Item.Text = string.Format("{0:n}", e.Item.AxisValue);
            }

        }

        private void chartControl1_CustomDrawSeries(object sender, CustomDrawSeriesEventArgs e)
        {
            //Color c;

            //if (e.Series.Name == "lnImport")
            //{
            //    c = Color.FromKnownColor(KnownColor.Blue);
            //}
            //else if (e.Series.Name == "lnExport")
            //{
            //    c = Color.FromKnownColor(KnownColor.Green);
            //}
            //else
            //{
            //    c = Color.FromKnownColor(KnownColor.Red);
            //}
            //e.SeriesDrawOptions.Color = c;
            //e.LegendDrawOptions.Color = e.SeriesDrawOptions.Color;

        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}